﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Models.ThreadPoolSamples
{
    using System.Threading;

    public static class Demo
    {
        public static void InvokeAll()
        {
            Action line = () =>
            {
                Console.ReadLine();
            };
            Invoke(QueueUserWorkItem, line);
        }
        public static void Invoke(params Action[] actions)
        {
            foreach (var action in actions)
            {
                action();
            }
        }
        public static void QueueUserWorkItem()
        {
            int workerThreads;
            int completionPortThreads;
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine("最大工作线程: {0}, I/O 完成线程: {1}", workerThreads, completionPortThreads);
            for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem((state) =>
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.WriteLine("循环 {0}, 池内运行的线程 {1}", j, Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(64);
                        }
                    });
            }
            Thread.Sleep(3000);
        }
    }
}
